package cn.shop.dys.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.adapter.RecommBrandApdapter;
import cn.shop.dys.bean.Brand;
import cn.shop.dys.bean.BrandInfo;
import cn.shop.dys.constant.CommonConstant;
import cn.shop.dys.constant.DBConstants;
import cn.shop.lib_common.CommonUtil;

/**
 * 推荐品牌----to 品牌专区
 *
 * @author zhangjun
 */
public class RecommmendBrandActivity extends AppCompatActivity {
    public static final String TAG = RecommmendBrandActivity.class.getSimpleName();
    private android.widget.TextView tvHuFu;
    private android.support.v7.widget.RecyclerView rcvHuFu;
    private android.widget.TextView tvCaiZhuang;
    private android.support.v7.widget.RecyclerView rcvCaiZhuang;
    private RecommBrandApdapter mHuFURecomBrandApater;
    private RecommBrandApdapter mCaiZhuangRecomBrandApater;
    private List<Brand> mBrands = new ArrayList<>();
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recommmend_brand);
        initView();
        initHeader();
    }

    public void backPre(View view) {
        finish();
    }

    private List<BrandInfo> mHUfuBrandInfos = new ArrayList<>();
    private List<BrandInfo> mCaiZhuangfuBrandInfos = new ArrayList<>();

    private void initView() {
        this.rcvCaiZhuang = (RecyclerView) findViewById(R.id.rcv_recombrand_caizhuang);
        this.tvCaiZhuang = (TextView) findViewById(R.id.tv_recombrand_caizhuang);
        this.rcvHuFu = (RecyclerView) findViewById(R.id.rcv_recombrand_hufu);
        this.tvHuFu = (TextView) findViewById(R.id.tv_recombrand_hufu);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
        rcvHuFu.setLayoutManager(layoutManager);
        rcvHuFu.addItemDecoration(new SpaceItemDecoration(20));

        StaggeredGridLayoutManager layoutManager2 = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL);
        rcvCaiZhuang.setLayoutManager(layoutManager2);
        rcvCaiZhuang.addItemDecoration(new SpaceItemDecoration(20));
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, CommonConstant.RECOMMEND_BRAND_URL, mCallBack);

    }

    private RequestCallBack<String> mCallBack = new RequestCallBack<String>() {

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            String result = responseInfo.result;
            Log.d(TAG, "onSuccess: " + result);
            try {
                JSONObject jsonObject = new JSONObject(result);
                String brandsJson = jsonObject.getString("brand");
                Gson gson = new Gson();
                Brand[] brandArray = gson.fromJson(brandsJson, Brand[].class);
                Log.d(TAG, "推荐品牌界面，请求数据json: " + brandArray.toString());
                for (Brand brand : brandArray) {
                    String key = brand.getKey();
                    if (key.equals("BB霜")){
                        mHUfuBrandInfos=brand.getValue();
                        setHuFuAdapter();
                        Log.d(TAG, "推荐品牌界面，护肤品牌集合长度 "+mHUfuBrandInfos.toString());
                    }else if (key.equals("洁面")){
                        mCaiZhuangfuBrandInfos=brand.getValue();
                        setCaiZhuangAdapter();
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(HttpException e, String s) {
            e.printStackTrace();
        }
    };

    private void setCaiZhuangAdapter() {
        if (mCaiZhuangRecomBrandApater == null) {
            mCaiZhuangRecomBrandApater = new RecommBrandApdapter(this, mCaiZhuangfuBrandInfos);
            rcvCaiZhuang.setAdapter(mCaiZhuangRecomBrandApater);
            mCaiZhuangRecomBrandApater.setOnRecyclerViewItemClickListener(new RecommBrandApdapter.OnRecyclerViewItemClickListener() {
                @Override
                public void onItemClick(View view, BrandInfo clickItemData) {
                    //TODO 将条目数据传递给品牌专区界面，分类界面
                    CommonUtil.showSafeToast(RecommmendBrandActivity.this, "彩妆品牌被点击了");

                }
            });
        } else {
            mCaiZhuangRecomBrandApater.notifyDataSetChanged();
        }

    }

    private void setHuFuAdapter() {
        if (mHuFURecomBrandApater == null) {
            Log.d(TAG, "推荐品牌界面,，---setHuFuAdapter-----集合长度--" + mHUfuBrandInfos.size());
            mHuFURecomBrandApater = new RecommBrandApdapter(this, mHUfuBrandInfos);
            rcvHuFu.setAdapter(mHuFURecomBrandApater);

        } else {
            mHuFURecomBrandApater.notifyDataSetChanged();
        }
    }

    private void initHeader() {
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText("推荐品牌");
    }

    /**
     * 设置recyclerView条目间距
     */
    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
        int mSpace;

        /**
         * @param space 传入的值，其单位视为dp
         */
        public SpaceItemDecoration(float space) {
            this.mSpace = (int) (CommonUtil.dpToPx(RecommmendBrandActivity.this, space));
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int itemCount = new RecommBrandApdapter().getItemCount();
            int pos = parent.getChildAdapterPosition(view);
            Log.d("推荐品牌适配器设置间距类", "itemCount>>" + itemCount + ";Position>>" + pos);

            outRect.left = 0;
            outRect.top = 0;
            outRect.bottom = 0;


            if (pos != (itemCount - 1)) {
                outRect.right = mSpace;
            } else {
                outRect.right = 0;
            }
        }
    }
}
